package com.yixiang.np.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yixiang.np.model.vo.OrganizationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yixiang.np.dao.OrganizationDao;
import com.yixiang.np.dao.UserDao;
import com.yixiang.np.model.Tree;
import com.yixiang.np.model.po.Organization;
import com.yixiang.np.service.OrganizationServiceI;


@Service
@Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
public class OrganizationServiceImpl implements OrganizationServiceI {
	
	SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd");
	@Autowired
	private UserDao userDao;

	@Autowired
	private OrganizationDao organ_dao;

	@Override
	public List<OrganizationVo> treeGrid() {
		List<OrganizationVo> list_organ = organ_dao.findAllVo();
		
		//List<Torganization> l = organizationDao
		//		.find("from Torganization t left join fetch t.organization  order by t.seq");
		
		if ((list_organ != null) && (list_organ.size() > 0)) {
			return list_organ;
		}
		return null;
	}

	@Override
	public void add(Organization org) {
		
		org.setCreatedatetime(new Date());
		organ_dao.add(org);		
	}

	@Override
	public void delete(Long id) {
		organ_dao.delete(id);
		
	}

	@Override
	public void edit(Organization org) {
		organ_dao.update(org);
		
	}

	@Override
	public Organization get(Long id) {
		return organ_dao.get(id);
	}

	@Override
	public List<Tree> tree() {
		/*List<Torganization> l = null;
		List<Tree> lt = new ArrayList<Tree>();

		l = organizationDao.find("select distinct t from Torganization t order by t.seq");
		 */
		List<Tree> list_tree = new ArrayList<Tree>(); 
		List<Organization> list =organ_dao.findByOrgid(null);
		if (list != null) {
			for (Organization org : list) {
				Tree tree = new Tree();
				tree.setId(org.getId().toString());
				tree.setAttributes(org.getPid());
				if(org.getPid()==null){
					tree.setPid(null);
				}else{
					tree.setPid(org.getPid().toString());
				}
				tree.setText(org.getName());
				tree.setIconCls(org.getIcon());
				tree.setChildren(getChildren(org.getId().intValue()));
				list_tree.add(tree);
			}
		}
		return list_tree;
	}
 
	public List<Tree> getChildren(Integer pid){
		List<Tree> list_tree = new ArrayList<Tree>(); 
		List<Organization> list =organ_dao.findByOrgid(pid);
		if (list != null) {
			for (Organization org : list) {
				Tree tree = new Tree();
				tree.setId(org.getId().toString());
				tree.setAttributes(org.getPid());
				if(org.getPid()==null){
					tree.setPid(null);
				}else{
					tree.setPid(org.getPid().toString());
				}
				tree.setText(org.getName());
				tree.setIconCls(org.getIcon());
				list_tree.add(tree);
			}
		}
		return list_tree;
	}
}
